package servlets;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import dao.DAOContact;
import dao.DAOGroup;
import domain.Address;
import domain.Contact;
import domain.ContactGroup;
import domain.Entreprise;
import domain.PhoneNumber;

/**
 * Servlet implementation class NewContactServlet
 */
public class NewContactServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewContactServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		org.springframework.context.ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		DAOContact dao = (DAOContact) context.getBean("contactDAO");
		String bean = request.getParameter("bean");
		// add contact with bean: stters way
		if (bean != null && bean.equals("1")) {
			Contact contact1 = (Contact) context.getBean("contact1");

			// add contact with bean: constructor way
			// Contact contact2 = (Contact) context.getBean("contact2");
			dao.addContact(contact1);
			// dao.addContact(contact2);

			RequestDispatcher rd = request
					.getRequestDispatcher("SearchContacts");
			rd.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("lastname");
		String email = request.getParameter("email");
		String street = request.getParameter("street");
		String city = request.getParameter("city");
		String zip = request.getParameter("zip");
		String country = request.getParameter("country");
		String phone1 = request.getParameter("phone1");
		String phone2 = request.getParameter("phone2");
		String phone3 = request.getParameter("phone3");
		// String groupName = request.getParameter("group");
		String creationType = request.getParameter("creationtype");

		String g1 = request.getParameter("friendChk");
		String g2 = request.getParameter("familyChk");
		String g3 = request.getParameter("colleagueChk");
		String gOther = request.getParameter("other");

		String numSiret = "";
		boolean isContact = true;
		if (creationType.equals("Enterprise")) {
			numSiret = request.getParameter("siret");
			isContact = false;
		}
		// create contact
		Entreprise contact = new Entreprise();
		contact.setFirstName(firstName);
		contact.setLastName(lastName);
		contact.setEmail(email);

		// create address
		Address addr = new Address();
		addr.setStreet(street);
		addr.setCity(city);
		addr.setZip(zip);
		addr.setCountry(country);
		contact.setAddress(addr);

		// create phones
		Set<PhoneNumber> phones = new HashSet<PhoneNumber>();
		PhoneNumber mobile1 = new PhoneNumber();
		mobile1.setPhoneKind(request.getParameter("phonetype1"));
		mobile1.setPhoneNumber(phone1);
		phones.add(mobile1);
		if (!phone2.equals("")) {
			PhoneNumber mobile2 = new PhoneNumber();
			mobile2.setPhoneKind(request.getParameter("phonetype2"));
			mobile2.setPhoneNumber(phone2);
			phones.add(mobile2);
		}
		if (!phone3.equals("")) {
			PhoneNumber mobile3 = new PhoneNumber();
			mobile3.setPhoneKind(request.getParameter("phonetype3"));
			mobile3.setPhoneNumber(phone3);
			phones.add(mobile3);
		}
		contact.setPhones(phones);

		org.springframework.context.ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		DAOContact dao = (DAOContact) context.getBean("contactDAO");

		DAOGroup daoGroup = (DAOGroup) context.getBean("daoGroup");
		List<String> groupName = daoGroup.search();
		// create contact group
		Set<ContactGroup> grps = new HashSet<ContactGroup>();

		if (g1 != null) {
			if (!groupName.contains(g1)) {
				ContactGroup grp = new ContactGroup();
				grp.setGroupName(g1);
				grps.add(grp);
			}
		}
		if (g2 != null) {
			if (!groupName.contains(g2)) {
				ContactGroup grp = new ContactGroup();
				grp.setGroupName(g2);
				grps.add(grp);
			}
		}
		if (g3 != null) {
			if (!groupName.contains(g3)) {
				ContactGroup grp = new ContactGroup();
				grp.setGroupName(g3);
				grps.add(grp);
			}
		}

		if (gOther != null && !gOther.isEmpty()) {
			if (!groupName.contains(gOther)) {
				ContactGroup grp = new ContactGroup();
				grp.setGroupName(gOther);
				grps.add(grp);
			}
		}

		contact.setBooks(grps);

		// DAOContact dao = new DAOContact();

		// add contact with bean: stters way
		// Contact contact1 = (Contact) context.getBean("contact1");

		// add contact with bean: constructor way
		// Contact contact2 = (Contact) context.getBean("contact2");

		// add contact with form
		if (isContact) {
			Contact c = new Contact();
			c.setFirstName(contact.getFirstName());
			c.setLastName(contact.getLastName());
			c.setEmail(contact.getEmail());
			c.setAddress(contact.getAddress());
			c.setBooks(contact.getBooks());
			c.setPhones(contact.getPhones());
			dao.addContact(c);
			if (groupName.contains(g1))
				daoGroup.createRefrence(c.getIdContact(), daoGroup.find(g1)
						.getGroupID());
			if (groupName.contains(g2))
				daoGroup.createRefrence(c.getIdContact(), daoGroup.find(g2)
						.getGroupID());
			if (groupName.contains(g3))
				daoGroup.createRefrence(c.getIdContact(), daoGroup.find(g3)
						.getGroupID());
			if (groupName.contains(gOther))
				daoGroup.createRefrence(c.getIdContact(), daoGroup.find(gOther)
						.getGroupID());

			request.setAttribute("contact",
					c.getFirstName() + " " + c.getLastName());

		} else {
			contact.setNumSiret(Integer.valueOf(numSiret));
			dao.addContact(contact);
			if (groupName.contains(g1))
				daoGroup.createRefrence(contact.getIdContact(),
						daoGroup.find(g1).getGroupID());
			if (groupName.contains(g2))
				daoGroup.createRefrence(contact.getIdContact(),
						daoGroup.find(g2).getGroupID());
			if (groupName.contains(g3))
				daoGroup.createRefrence(contact.getIdContact(),
						daoGroup.find(g3).getGroupID());
			if (groupName.contains(gOther))
				daoGroup.createRefrence(contact.getIdContact(),
						daoGroup.find(gOther).getGroupID());

			request.setAttribute("contact", contact.getFirstName() + " "
					+ contact.getLastName());
		}

		RequestDispatcher rd = request
				.getRequestDispatcher("pages/personalPage.jsp");
		rd.forward(request, response);
	}

	// public void addReference(DAOGroup daoGroup,List<String> groupName){
	//
	// }
}
